<?php
// 应用公共文件
function cUrl($url, $post_string, $method = 'post', $type = '', $headers = [], $timeout = 30) {
    $header = [];
    if ($method == 'get')  $url .= '?' . $post_string;
    if ($type === 'json') {
        $header[] = 'content-type: application/json; charset=UTF-8';
        $header[] = 'Content-Length: ' . strlen($post_string);
    }
    if (!empty($headers)) $header = array_merge($header, $headers);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    }

    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch,CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $code = 1;
    $info = '';
    if ($output === false) {
        $code = 0;
        $info = curl_error($ch);
    }
    curl_close($ch);
    return ['code' => $code, 'info' => $info, 'data' => $output];
}

function isValidDateFormat($dateString, $format = 'Y-m-d H:i:s') {
    $date = DateTime::createFromFormat($format, $dateString);
    return $date !== false && $date->format($format) == $dateString;
}
